import config_pb2


def serializition(config: config_pb2.PB_TerninalConfigration) -> bytes:
    """序列化配置信息"""
    return config.SerializeToString()


def deserializition(data: bytes) -> config_pb2.PB_TerninalConfigration:
    """反序列化配置信息"""
    config = config_pb2.PB_TerninalConfigration()
    config.ParseFromString(data)
    return config


def main():
    """测试"""
    config = config_pb2.PB_TerninalConfigration()

    device_info = config_pb2.PB_DeviceInfo()
    device_info.terminal_sn = "123456789"
    device_info.log_level = 2
    config.device_info.CopyFrom(device_info)

    server_info = config_pb2.PB_ServerInfo()
    server_info.hostname = "dengyongsheng.cn"
    server_info.port = 8080
    config.server_info.CopyFrom(server_info)

    net_info = config_pb2.PB_NetInfo()
    net_info.mac_addr = b'\xff\x00\x11\x22\x33'
    net_info.use_dhcp = True
    # ip地址
    net_info.ip = int.from_bytes(
        [192, 168, 1, 2], byteorder='big', signed=False)
    # 网关地址
    net_info.gateway = int.from_bytes(
        [192, 168, 1, 1], byteorder='big', signed=False)
    # 子网掩码
    net_info.subnet_mask = int.from_bytes(
        [255, 255, 255, 0], byteorder='big', signed=False)
    # dns地址
    net_info.dns = int.from_bytes(
        [8, 8, 8, 8], byteorder='big', signed=False)
    config.net_info.CopyFrom(net_info)

    data = serializition(config)

    print("{:*^40}".format('原始信息'))
    print(config)

    print("{:*^40}".format('二进制数据'))
    print(data)

    print("{:*^40}".format('反序列化后的信息'))
    config2 = deserializition(data)
    print(config2)


if __name__ == "__main__":
    main()
